package algorithm;

import java.util.Arrays;

/**
 * 排序算法
 *
 * @author luoyalan
 * @date 2018/7/22
 */
public class Sort {
    /**
     * 桶排序：适用于数据取值范围小、无重复数据的情况
     *
     * @param arr
     * @return
     */
    public static int[] bucketSort(int[] arr) {
        if (arr == null || arr.length < 1) {
            return null;
        }
        int[] bucket = new int[100];
        for (int i = 0; i < bucket.length; i++) {
            bucket[i] = 0;
        }
        for (int j = 0; j < arr.length; j++) {
            // 当待排序数据中存在重复数据时，会有问题
            bucket[arr[j]] = 1;
        }
        for (int k = 0; k < bucket.length; k++) {
            if (bucket[k] != 0) {
                System.out.println(k);
            }
        }
        return bucket;
    }

    /**
     * 选择排序：每次找到最小或最大的值，放到有序的一边
     *
     * @param arr
     * @return
     */
    public static int[] chooseSort(int[] arr) {
        if (arr == null || arr.length < 1) {
            return null;
        }
        for (int i = 0; i < arr.length; i++) {
            int index = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[index] > arr[j]) {
                    index = j;
                }
            }
            int temp = arr[index];
            arr[index] = arr[i];
            arr[i] = temp;
        }
        return arr;
    }

    /**
     * 冒泡排序：比较相邻的元素，进行交换
     *
     * @param arr
     * @return
     */
    public static int[] bubbleSort(int[] arr) {
        if (arr == null || arr.length < 1) {
            return null;
        }
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                int index = j + 1;
                if (arr[j] > arr[index]) {
                    int temp = arr[j];
                    arr[j] = arr[index];
                    arr[index] = temp;
                }
            }
        }
        return arr;
    }

    /**
     * 插入排序
     *
     * @param unsorted
     * @return
     */
    public static int[] insertSort(int[] unsorted) {
        if (unsorted == null || unsorted.length < 1) {
            return null;
        }
        for (int i = 1; i < unsorted.length; i++) {
            if (unsorted[i - 1] > unsorted[i]) {
                int temp = unsorted[i];
                int j = i;
                while (j > 0 && unsorted[j - 1] > temp) {
                    unsorted[j] = unsorted[j - 1];
                    j--;
                }
                unsorted[j] = temp;
            }
        }
        return unsorted;
    }

    /**
     * 归并排序:将两个已经有序的序列合并
     *
     * @param unsortArr1 已经排好序的数组
     * @param unsortArr2 已经排好序的数组
     * @return
     */
    public static int[] mergerSort(int[] unsortArr1, int[] unsortArr2) {
        if (unsortArr1 == null || unsortArr1.length < 1) {
            return insertSort(unsortArr2);
        }
        if (unsortArr2 == null || unsortArr2.length < 1) {
            return insertSort(unsortArr1);
        }
        int[] result = new int[unsortArr1.length + unsortArr2.length];

        int indexArr1 = 0;
        int indexArr2 = 0;
        int temp = 0;
        for (int i = 0; i < result.length; i++) {
            if (indexArr1 == unsortArr1.length) {
                if (indexArr2 < unsortArr2.length) {
                    result[i] = unsortArr2[indexArr2++];
                    continue;
                }
            }

            if (indexArr2 == unsortArr2.length) {
                if (indexArr1 < unsortArr1.length) {
                    result[i] = unsortArr1[indexArr1++];
                    continue;
                }
            }

            if (unsortArr1[indexArr1] < unsortArr2[indexArr2]) {
                temp = unsortArr1[indexArr1];
                indexArr1++;
            } else {
                temp = unsortArr2[indexArr2];
                indexArr2++;
            }
            result[i] = temp;
        }
        return result;
    }

    /**
     * 快速排序
     *
     * @param unsorted
     * @return
     */
    public static int[] quickSort(int[] unsorted) {
        if (unsorted == null || unsorted.length < 1) {
            return null;
        }
        // TODO



        return null;
    }


    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        int[] arr2 = {6, 7, 8, 9, 10};
        // int[] bucket = bucketSort(arr);
        // System.out.println(Arrays.toString(bucket));
        int[] result = mergerSort(arr, arr2);

        // arr = insertSort(arr);
        System.out.println(Arrays.toString(result));
    }
}
